#lang racket

(provide make-numberizer numberize! numberizer-length clone-numberizer)

;; > (define x (make-numberizer))
;; > (numberize! x 'foo)
;; 0
;; > (numberize! x 'bar)
;; 1
;; > (numberize! x 'foo)
;; 0
;; > (numberize! x 'bar)
;; 1
;; > (numberize! x 'baz)
;; 2

(define (make-numberizer) (box '()))

(define (numberize! nb thing)
 (let loop ((i 0) (l (unbox nb)))
  (if (null? l)
      (begin (set-box! nb (append (unbox nb) (list thing)))
             i)
      (if (equal? thing (car l))
          i
          (loop (+ i 1) (cdr l))))))

(define (numberizer-length nb) (length (unbox nb)))

(define (clone-list l)
  (map (lambda (x) x) l))

(define (clone-numberizer n)
  (box (clone-list (unbox n))))
